package app

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jassue/jassue-gin/app/common/request"
	"github.com/jassue/jassue-gin/app/common/response"
	"github.com/jassue/jassue-gin/app/services"
	"strconv"
)

// 添加菜单
func AddMenu(c *gin.Context) {

	var form request.AddMenu
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err := services.MenuService.AddMenu(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 编辑菜单
func EditMenu(c *gin.Context) {
	var form request.EditMenu
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err := services.MenuService.EditMenu(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 菜单排序
func SetMenuSort(c *gin.Context) {

	var form request.SetMenuSort
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err := services.MenuService.SetMenuSort(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 删除菜单
func DelMenu(c *gin.Context) {
	var form request.Del
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err := services.MenuService.Del(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 所有菜单列表 菜单和按钮 树形结构
func GetMenuList(c *gin.Context) {

	var form request.GetMenuList
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err, arr := services.MenuService.GetMenuList(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, arr)
	}

}

// 动态获取用户菜单
func Getmenu(c *gin.Context) {

	uid := c.Keys["id"].(string)
	intId, _ := strconv.Atoi(uid)
	fmt.Printf("intId:%#v\n", intId)
	if intId == 0 {
		response.BusinessFail(c, "未登录")
	}

	if err, result := services.MenuService.GetMenu(intId); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, result)
	}
}

// 获取用户所有按钮权限接口
func GetButtonPermissions(c *gin.Context) {

	uid := c.Keys["id"].(string)
	intId, _ := strconv.Atoi(uid)
	fmt.Printf("intId:%#v\n", intId)
	if intId == 0 {
		response.BusinessFail(c, "未登录")
	}
	if err, result := services.MenuService.GetButtonPermissions(intId); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, result)
	}

}
